// Miniboss.cpp //
#include "Miniboss.h"

#include "math.h"
#include "Bullet.h"
#include "Player.h"
#ifndef M_PI
#define M_PI           3.14159265358979323846
#endif

// creates mini bosses, spawned by the actual boss

Miniboss::Miniboss(string keyname) : Enemy(keyname) // constructor
{
	int rPrime = rand() % 34; // initial prime
	int newPrime = Primes[rPrime];
	this->numValue = newPrime;
	pointValue = MinibossPointValue;
	health = MinibossHealth;
	position = Vector(0, -ScreenHeight / 2);

	lastFired = 0.0f;
}

void Miniboss::Update(float deltaTime)
{
	if(this->numValue == -1) // if hit and made divisable
	{
		this->TakeDamage(300);
		int rPrime = rand() % 34;
		int newPrime = Primes[rPrime];
		this->numValue = newPrime;
	}
	Enemy::Update(deltaTime);
	
	lastFired += deltaTime; // controls fire rate
	if(lastFired > MinibossFireDelay)
	    FireBullet();
}

void Miniboss::RemoveOffScreen()
{
	SDL_Rect bounds = {-ScreenWidth, -ScreenHeight, ScreenWidth * 3, ScreenHeight * 3};
	SDL_Rect intersect = Intersection(GetBounds(), bounds);

	if(intersect.w == 0 && intersect.h == 0)
	    Remove();
}

// fire bullets in an octagon shape pattern
void Miniboss::FireBullet()
{
	for(int i = 0; i < 8; i++)
	{
		Bullet* bullet = new Bullet("Mini");
	    bullet->position = position;
	    bullet->direction = Vector::CalculateDirection(i * (M_PI / 4) + (M_PI / 8));
	    bullet->speed = 150.0f;
	    bullet->CollisionList = &PlayerShip::PlayerShipList;
	}

    lastFired = 0.0f;
}

//renders miniboss with number
void Miniboss::Render(SDL_Surface* screen)
{
	char buffer[4];
	sprintf(buffer, "%d", this->numValue);
	sprite->Render(screen,position);
	sprite->texture->DrawText(screen,buffer,50,this->position.X,this->position.Y,0,0,0,true);
}
